package com.cloud.gatewaySpringSecurity.handler;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.authentication.ServerAuthenticationConverter;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author zhozixiong
 * @date 2023/03/02
 *
 * 手机号登录参数处理器
 */
@Component
public class PhoneHandler implements ServerAuthenticationConverter {

    @Override
    public Mono<Authentication> convert(ServerWebExchange exchange) {
        MultiValueMap<String, String> map = exchange.getRequest().getQueryParams();
        String phone = map.get("phone").get(0);
        String code = map.get("code").get(0);
        if(phone == null) {
            phone = "";
        }
        if(code == null) {
            code = "";
        }
        AccountCodeAuthenticationToken authenticationToken = new AccountCodeAuthenticationToken(phone, code);
        return Mono.just(authenticationToken);
    }
}
